package com.mango.json;

import com.alibaba.fastjson.JSONObject;
import org.junit.platform.commons.util.StringUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;



/**
 * 获取产品分类DEMO
 */
public class ProductTypeDemo {

    public static void main(String[] args) throws IOException {
        //第一波过滤无用数据
        List<ProductType> productTypeList = filtrationOne();
        //提取分类名称 总数据
        for (ProductType productType : productTypeList) {
            if (productType.getHasChildren()) {
                //子节点ID
                List<String> childNodes = productType.getChildNodes();
                //过滤出数据
                List<ProductType> productTypes = productTypeList.stream().filter(item -> {
                    return childNodes.contains(item.getBrowseNodeId());
                }).collect(Collectors.toList());
                productType.setChildProductTypeList(productTypes);
            }
        }
        //过滤出一级类目
        Set<String> Lv1List2 = new HashSet<>();
        productTypeList.stream().forEach(item -> {
            Lv1List2.add(item.getBrowsePathByName().split(",")[0]);
        });
        Lv1List2.add("Arts, Crafts & Sewing");

        //只获取包含第一类目数据
        List<ProductType> data = productTypeList.stream().filter(item -> {
            return Lv1List2.contains(item.getBrowsePathByName())  && item.getHasChildren() == true ;
        }).collect(Collectors.toList());

        //生产JSON文件
        createFile("productTypeTree",JSONObject.toJSONString(data,true));


        System.out.println("");

    }


    public static List<ProductType> filtrationOne() {
        String path = "D:\\AMAZON_CATEGORY.json";
        File file = new File(path);
        String str = Test.getStr(file);
        //解析JSON
        List<ProductType> productTypeList = JSONObject.parseArray(str, ProductType.class);
        //查找所有没有子节点的数据
        System.out.println("-----总节点个数: " + productTypeList.size());
        System.out.println("=======开始过滤数据,条件: productTypeDefinitions == null  || productTypeDefinitions =='WEB_SERVICES'=========");
        List<ProductType> collect = productTypeList.stream().filter(item -> {
            return StringUtils.isNotBlank(item.getProductTypeDefinitions());
        }).collect(Collectors.toList());
        return collect;
    }


    /**
     * 文件生成
     */
    public static void createFile(String fileName, String text) throws IOException {
        String filePath = "D:\\xsd\\jsonTree\\" + fileName + ".json";

        Path path = Paths.get(filePath);
        // 使用newBufferedWriter创建文件并写文件
        // 这里使用了try-with-resources方法来关闭流，不用手动关闭
        try (BufferedWriter writer =
                     Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
            writer.write(text);
        } catch (IOException e) {
            throw e;
        }
    }

}